package com.project.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.project.bean.EmployeeBean;
import com.project.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * @Title: EmController
 * @Package: com.project.controller
 * @Description:
 * @Author: Liang-Xie
 * @Date: 2022/6/30 - 11:32
 */
@SuppressWarnings("all")
@RestController
@RequestMapping("em")
public class EmController {
    @Autowired
    private IEmployeeService service;
    @RequestMapping("findAll")
    public List<EmployeeBean>findAll(){
        return service.findAll();
    }
    @RequestMapping("dismission")
    public String dismission(Integer id){
        service.dismission(id);
        return "ok";
    }
    @RequestMapping("findById")
    public EmployeeBean findById(Integer id){
        return service.findById(id);
    }
    @RequestMapping("add")
    public String add(@Valid EmployeeBean em, BindingResult br) throws JsonProcessingException {
        if (br.hasErrors()){
         List<FieldError> errorList= br.getFieldErrors();
            ObjectMapper om=new ObjectMapper();
            String erroInfo=om.writeValueAsString(errorList);
            return erroInfo;
        }

        service.add(em);
        return "ok";
    }
    @RequestMapping("update")
    public String update(Integer id,Integer money){
        service.update(id,money);
        return "ok";
    }
}
